package com.alcatel.axs.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.alcatel.axs.dao.ServerDAO;
import com.alcatel.axs.model.Server;


@Component
public class ServerDAOImpl implements ServerDAO {
    private static final Logger m_logger = Logger.getLogger(ServerDAOImpl.class);
    
    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory m_sessionFactory;
    
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        m_sessionFactory = sessionFactory;
    }

    @Autowired
    public ServerDAOImpl(SessionFactory sessionFactory) {
        m_sessionFactory = sessionFactory;
    }
    

    public Session openSession() throws SQLException {
        return m_sessionFactory.openSession();
    }

    @Override
    public void insert(Server server) {
        Session session =null;
        try {
            session = openSession();
            session.beginTransaction();
            session.save(server);
            session.getTransaction().commit();
        } catch (Exception ex) {
            m_logger.error(ex.getMessage(), ex);
        } finally {
            session.close();
        }

    }

    @Override
    public boolean findByIp(Server s) {
        Session session=null;
        try {
            session= openSession();
            session.beginTransaction();
            Query query = session.createQuery(String.format("from Server where ip='%s'",s.getIp()));
            List result = query.list();
            
            if(!result.isEmpty()) {
                return true;
            }
            
        } catch (Exception ex) {
            m_logger.error(ex.getMessage(), ex);
        } finally {
           session.close();
        }
        return false;
    }

    @Override
    public List<Server> getServer() {
        Session session=null;
        List<Server> serverList=new ArrayList<Server>();
        try {
            session= openSession();
            session.beginTransaction();
            Query query = session.createQuery(String.format("from Server order by ip desc"));
            List result = query.list();
            for (int i = 0; i < result.size(); i++) {
                Server server= (Server) result.get(i);
                serverList.add(server);
            }
        } catch (Exception ex) {
            m_logger.error(ex.getMessage(), ex);
        } finally {
            session.close();
        }
        return serverList;
    }
    
    @Override
    public boolean updateStatusByIp(Server server) {
        int updated = 0;
        Session session=null;
        try {
            session= openSession();
            session.beginTransaction();
            Query query = session.createQuery(String.format("UPDATE Server SET status='%s' WHERE ip='%s'",
                    server.getStatus().toString(), server.getIp()));
            updated = query.executeUpdate();
        } catch (Exception ex) {
            m_logger.error(ex.getMessage(), ex);
        } finally {
            session.close();
        }
        return updated == 1;
    }

}
